package com.henu.engineer.service.teacher

import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.henu.engineer.dao.entity.student.StudentComprehensiveScore
import com.henu.engineer.dao.mapper.student.StudentComprehensiveScoreXmlMapper
import com.henu.engineer.dao.mapper.student.StudentCourseMethodXmlMapper
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.stream.Collectors


/**
 * 查看所在课程组的课程的所有学生的成绩，包括该门课程的各个考核方式的成绩，综合成绩。可以编辑个别成绩信息。
 * @author noexcept
 * @since 2021/8/12 22:23
 */
@Service
class StudentCourseScoreService {

    val logger: Logger = LoggerFactory.getLogger(javaClass)

    @Autowired
    lateinit var studentComprehensiveScoreXmlMapper: StudentComprehensiveScoreXmlMapper

    @Autowired
    lateinit var studentCourseMethodXmlMapper: StudentCourseMethodXmlMapper

    /**
     * 返回所有学生某门课程的综合成绩
     */
    fun getCourseComprehensiveScoreOfAllStudent(courseId: String): JSONObject {

        val rawData = studentComprehensiveScoreXmlMapper.selectByCourseId(courseId)
        val res = JSONObject()
        if (rawData.isEmpty())
            return res
        res["courseId"] = rawData.first().courseId
        res["courseName"] = rawData.first().courseName
        val data = JSONArray()
        rawData.forEach {
            val jsonObject = JSONObject().apply {
                put("studentId", it.studentId)
                put("studentName", it.studentName)
                put("comprehensiveScore", it.score)
            }
            data.add(jsonObject)
        }
        res["data"] = data
        res["count"] = data.size
        return res
    }

    /**
     * 某课程所有学生的各考核方式成绩
     *
     */
    fun getCourseMethodScoreOfAllStudent(courseId: String): JSONObject {
        val rawData =
            studentCourseMethodXmlMapper.selectCourseMethodScoreOfAllStudent(courseId)

        val rawData2 = studentComprehensiveScoreXmlMapper.selectByCourseId(courseId)
        val comprehensiveScoreMap = rawData2.stream().collect(Collectors.toMap(StudentComprehensiveScore::studentId) {
            it.score
        })

        val res = JSONObject()
        if (rawData.isEmpty())
            return res

        val first = rawData.first()
        res.apply {
            put("courseId", courseId)
            put("courseName", first.courseName)
        }
        val jsonArray = JSONArray()

        rawData.groupBy { it.studentId }.forEach {
            val list = it.value
            list.first()
            val singleStudent = JSONObject().apply {
                put("id", it.key)
                put("name", list.first().studentName)
                put("classNum", list.first().classNum)
                put("semester", list.first().semester)
                put("comprehensiveScore", comprehensiveScoreMap[list.first().studentId])
            }

            val methodScores = JSONArray()
            list.forEach {
                val singleMethodScore = JSONObject().apply {
                    put("method", it.method)
                    put("score", it.score)
                }
                methodScores.add(singleMethodScore)
            }
            singleStudent["data"] = methodScores
            singleStudent["count"] = methodScores.size
            jsonArray.add(singleStudent)
        }
        res["data"] = jsonArray
        res["count"] = jsonArray.size

        return res
    }

    /**
     * 修改某个学生的某个课程的某个考核方式成绩
     */
    fun updateCourseMethodScoreOfAStudent(courseId: String, studentId: String, method: String, score: Double) =
        studentCourseMethodXmlMapper.updateSingle(studentId, courseId, method, score)
}